﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public enum CommandType : int
{
    Unit_Move,
    Unit_Attack 
} 

public class BaseCommand
{
    public CommandType CommandType;
    public object[] Args;
}

public class CommandQueue
{
    LinkedList<BaseCommand> commands = new LinkedList<BaseCommand>();
    Dictionary<CommandType, List<Action<object[]>>> PushEvents;
    public CommandQueue() 
    {
        this.PushEvents = new Dictionary<CommandType, List<Action<object[]>>>();
    }

    public void AddEvent(CommandType typ,Action<object[]> fn) 
    {
        if (!this.PushEvents.ContainsKey(typ)) 
        {
           this.PushEvents[typ] = new List<Action<object[]>>();
        }
        this.PushEvents[typ].Add(fn);
    }

    void ExecEvent(BaseCommand command) 
    {
        if (this.PushEvents.ContainsKey(command.CommandType))
        {
            foreach (Action<object[]> fn in this.PushEvents[command.CommandType])
            {
                fn(command.Args);
            }
        }
    }
    public BaseCommand Current()
    {
        return commands.First.Value;
    }

    public BaseCommand Push(CommandType commandType,params object[] args)
    {
        BaseCommand cmd = new BaseCommand();
        cmd.CommandType = commandType;
        cmd.Args = args;
        this.commands.AddLast(cmd);
        if (this.commands.Count == 1) 
        {
            ExecEvent(cmd);
        }
        return cmd;
    }
    public void Pop()
    {
        this.commands.RemoveFirst();
        if(this.commands.Count>0)
        {
            ExecEvent(this.Current());
        }
    }

    public void Clear() 
    {
        this.commands.Clear();
    }
}


